#ifndef __COMM_HPP__
#define __COMM_HPP__

#include <iostream>
#include <string>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
using namespace std;

#define MODE 0666
#define Path "./fifo"

class Fifo
{
public:
    Fifo(const string path):_path(path)
    {   
        umask(0);
        int n = mkfifo(_path.c_str(), MODE);//创建命名管道
        if(n == 0) cout<< "mkfifo success" << endl;
        else cerr<< "mkfifo failed errno " << errno << " , errstring: " << strerror(errno) << endl;
    }
    ~Fifo()
    {
        int n = unlink(_path.c_str());
        if(n == 0) cout<< "remove fifo fie " << _path << " success" << endl;
        else cerr << "remove failed, error " << errno << " , errstring is " << strerror(errno) << endl;
    }
private:
    string _path;
};

class Sync
{
public:
    // Sync(){}
    // ~Sync(){}
    void OpenReadOrDie()
    {
        rfd = open(Path,O_RDONLY);
        if(rfd < 0) exit(1);
    }

    void OpenWriteOrDie()
    {
        wfd = open(Path,O_WRONLY);
        if(wfd < 0) exit(2);
    }

    bool Wait()
    {
        bool ret = true;
        uint32_t c = 0;
        ssize_t n = read(rfd, &c, sizeof(uint32_t));
        if(n == sizeof(uint32_t))
        {
            cout << "server wakeup, begin read shm..." << endl;
        }
        else if(n<=0) ret = false;
        return ret;
    }

    void Wakeup()
    {
        uint32_t c = 0;
        ssize_t n = write(wfd, &c, sizeof(c));
        assert(n == sizeof(uint32_t));
        cout << "wakeup server..." << endl;
    }
private:
    int rfd = -1;
    int wfd = -1;
};

#endif